package com.kwan80gmail.flextime.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.kwan80gmail.flextime.R;
import com.kwan80gmail.flextime.controllers.MainController;
import com.kwan80gmail.flextime.models.Place;
import com.kwan80gmail.flextime.models.SqlLiteHelper;

import java.util.ArrayList;
import java.util.List;

public class PlacesActivity extends ActionBarActivity
{
    ListView locationListView;
    List<String> places = new ArrayList<>();
    ArrayAdapter<String> adapter;
    MainController controller = new MainController();
    SqlLiteHelper database;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places);

        locationListView = (ListView) findViewById(R.id.locationList);
        database = controller.getDatabase(this);

        for(Place place : database.getPlaces())
        {
            places.add(place.getName());
        }
        ;

        //kopplar data till adaptern
        adapter =  new ArrayAdapter<>(this, android.R.layout.simple_list_item_activated_1, places);

        locationListView.setAdapter(adapter);
        locationListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent intent = new Intent("placeSettingsActivity");
                String placeName = adapter.getItem(position);

                intent.putExtra("Place", database.getPlaceByName(placeName));

                startActivity(intent);
              // changeActivity("PlaceSettingsActivity");

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_location, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void changeActivity(String activityName)
    {
        startActivity(new Intent(activityName));
    }
}
